<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>集合的实践</title>
</head>
<body>

</body>
<script type="text/javascript">
    //1. 数组去重
    // let arr = ['大事儿','小事儿','好事儿','坏事儿','小事儿'];
    // let res = [...new Set(arr)];
    // console.log(res);

    //2. 交集
    let mFriend = ['思思','琪琪', '芳芳','畅畅','畅畅']
    let yuFriend = ['思思', '琪琪','畅畅','秋香'];

    let interFriend = [...new Set(mFriend)].filter(item=> {
        //将数组转为集合
        let s = new Set(yuFriend);
        //检测 item 是否存在于 s 中
        // if(s.has(item)) {
        //     return true;
        // }else{
        //     return false;
        // }
        return s.has(item);
    });

    //3. 并集
    // let unionFriend = [...new Set([...mFriend, ...yuFriend])];
    // console.log(unionFriend);

    //4. 差集
    // [1,2,3] - [3,4,5]
    // [3,4,5] - [1,2,3]

    let diffFriend = [...new Set(yuFriend)].filter(item => {
        let s = new Set(mFriend);
        //判断
        if(s.has(item)){
            return false;
        }else{
            return true;
        }
    })

    console.log(diffFriend);

</script>
</html>